package com.demo.petshop.service.impl;

import com.demo.petshop.entity.Users;
import com.demo.petshop.mapper.UsersMapper;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import static com.demo.petshop.entity.table.UsersTableDef.USERS;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    UsersMapper usersMapper;

    @Override
    public UserDetails loadUserByUsername(String info) throws UsernameNotFoundException {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.where(USERS.USERNAME.eq(info))
                .or(USERS.PHONE.eq(info))
                .or(USERS.EMAIL.eq(info));
        Users user = usersMapper.selectOneByQuery(queryWrapper);
        if(user == null){
            return null;
        }
        return new LoginUser(user);
    }
}
